package calao.client;

import com.google.gwt.user.client.ui.PasswordTextBox;

import calao.client.i18n.Text;
import calao.client.ui.Form;
import calao.client.ui.HasTitle;
import calao.client.ui.InvalidInputException;
import calao.client.ui.RegexTextBox;


public class MemberWizard 
extends Form
implements HasTitle
{
		
	final Text text = Desktop.getDesktop().text;
	final UserAdminPanel parent;
	
	// first step
	final RegexTextBox nameBox = new RegexTextBox(".+");
	final RegexTextBox aliasBox = new RegexTextBox(".+");
	final RegexTextBox emailBox = new RegexTextBox(".+@.+");
	final PasswordTextBox passwdBox = new PasswordTextBox();
	final PasswordTextBox confirmBox = new PasswordTextBox();
	
	MemberWizard(UserAdminPanel parent)
	{
		super(CREATE);
		this.parent = parent;
		nameBox.setVisibleLength(80);
		aliasBox.setVisibleLength(40);
		emailBox.setVisibleLength(40);
		add(text.name(), nameBox);
		add(text.userAlias(), aliasBox);
		add(text.email(), emailBox);
		add(text.password(), passwdBox);
		add(text.confirm(), confirmBox);
	}
	
	public String getTitle()
	{
		return text.newUser();
	}
	
	public void create()
	{
		MemberInfo member = new MemberInfo();
		member.name = nameBox.getText();
		member.alias = aliasBox.getText();
		member.email = emailBox.getText();
		member.password = passwdBox.getText();
		Desktop.getDesktop().memberService.create(member, new ServiceCallback() {
			protected void onReturn(Object result) {
				Desktop.getDesktop().popView();
				Desktop.getDesktop().stackView(new MemberPanel(parent, (MemberInfo)result));
				parent.refresh();
			}
		});
	}
	
	public void validate() throws InvalidInputException
	{
		if (!confirmBox.getText().equals(passwdBox.getText())) {
			throw new InvalidInputException(text.passwordNotConfirmed());
		}
	}
		
}
